package com.zjx.shop.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.shop.common.utils.PageParams;
import com.zjx.shop.product.entity.SpuInfoEntity;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.shop.common.utils.PageUtils;
import com.zjx.shop.product.dao.SkuInfoDao;
import com.zjx.shop.product.entity.SkuInfoEntity;
import com.zjx.shop.product.service.SkuInfoService;
import org.springframework.util.StringUtils;

import java.util.List;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(PageParams params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(params.getKey())){
            wrapper.like("sku_name", params.getKey());
        }
        if (params.getCatelogId() != 0){
            wrapper.eq("catalog_id",params.getCatelogId());
        }
        if (params.getBrandId() != 0){
            wrapper.eq("brand_id",params.getBrandId());
        }
        if (params.getMin() != 0 && params.getMax() != 0){
            wrapper.between("price",params.getMin(),params.getMax());
        }

        IPage<SkuInfoEntity> page = this.page(
                new Page<>(params.getPage(),params.getSize()),
                wrapper
        );

        return new PageUtils(page.getCurrent(),page.getSize(),(long) page.getRecords().size(),page.getRecords());
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        QueryWrapper<SkuInfoEntity> wrapper =new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);
        return this.baseMapper.selectList(wrapper);
    }
}